package com.wtb.javatool.service.impl;

import com.wtb.javatool.dao.LineMapper;
import com.wtb.javatool.dto.LineDto;
import com.wtb.javatool.entity.Line;
import com.wtb.javatool.entity.LineAssignment;
import com.wtb.javatool.service.LineService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LineServiceImpl implements LineService {
    private final LineMapper lineMapper;

    public LineServiceImpl(LineMapper lineMapper) {
        this.lineMapper = lineMapper;
    }


    /**
     * 新增项目产线
     *
     * @param lineName   产线名称
     * @param projectId  项目Id
     * @param createTime 创建时间
     */
    @Override
    public void addLine(String lineName, Long projectId, Date createTime) {
        Line line = new Line();
        line.setLineName(lineName);
        line.setCreateTime(createTime);
        line.setProjectId(projectId);
        lineMapper.addLine(line);
    }

    /**
     * 根据项目id 查询所有项目产线详情
     *
     * @param projectId 项目Id
     * @return LineDTO列表
     */
    @Override
    public List<LineDto> findLinesByProjectId(Long projectId, Integer projectType) {
        List<Long> lineIdS = lineMapper.findLineIdsByProjectId(projectId);
        if (lineIdS.isEmpty())
            return null;
        else if (projectType == 1) {
            return lineMapper.findNormalLinesByLineIds(lineIdS);
        } else {
            return lineMapper.findTeachingLinesByLineIds(lineIdS);
        }

    }

    /**
     * 根据产线id 查询产线详情
     *
     * @param lineId 产线Id
     * @return LineDTO
     */
    @Override
    public LineDto findLineByLineId(Long lineId) {
        return lineMapper.findLineByLineId(lineId);
    }

    /**
     * 更新产线节点
     *
     * @param lineId             产线ID
     * @param lineAssignmentList 产线节点列表
     */
    @Override
    public void updateLineAssignment(Long lineId, List<LineAssignment> lineAssignmentList) {
        lineMapper.deleteLineAssignments(lineId);
        if (!lineAssignmentList.isEmpty())
            lineMapper.addLineAssignments(lineId, lineAssignmentList);
    }

    /**
     * 删除产线与其关联的记录
     *
     * @param lineId 产线id
     */
    @Override
    public void deleteLine(Long lineId) {
        lineMapper.deleteLineAssignments(lineId);
        lineMapper.deleteLine(lineId);
    }

    /**
     * 修改产线名称
     *
     * @param lineId   产线id
     * @param lineName 产线名称
     */
    public void updateLineName(Long lineId, String lineName) {
        lineMapper.updateLineName(lineId, lineName);
    }

    /**
     * 复制产线及其节点
     *
     * @param lineId     被复制的产线ID
     * @param lineName   新产线名称
     * @param projectId  项目id
     * @param createTime 创建时间
     */
    @Override
    public void copyLine(Long lineId, String lineName, Long projectId, Date createTime) {
        List<LineAssignment> lineAssignmentList = lineMapper.findLineAssignmentByLineId(lineId);
        Line line = new Line();
        line.setLineName(lineName);
        line.setCreateTime(createTime);
        line.setProjectId(projectId);
        lineMapper.addLine(line);
        System.out.println(line.getLineId());

        lineMapper.addLineAssignments(line.getLineId(), lineAssignmentList);
    }

    /**
     * 查询分配记录是否被作为产线节点
     *
     * @param assignmentIds 分配记录ID列表
     * @return 返回分配记录中被作为节点的个数
     */
    public int findLineAssignmentByAssignmentIds(List<Long> assignmentIds) {
        return lineMapper.findLineAssignmentByAssignmentIds(assignmentIds);
    }
}
